package ModStats;

import java.util.List;

public class CalculStats {
	
	/*M�thode de calcul de la m�diane
	 * si n est impaire median=f ((n+1)/2
	 * si n est paire median = 1/2 [ f(n/2)+ f((n+1)/2) ]
	 */
	public static double median(ValuesList listData){
		double result = 0;
		double half1, half2;
		List<Double> listDt;
		listDt=listData.sortValues();
		if(listDt.size()%2!=0){
			result=listDt.get(((listDt.size()+1)/2)-1);	
		}
		else{
			half1=listDt.get((listDt.size()/2)-1);
			half2=listDt.get(((listDt.size()/2)+1)-1);
			result=(half1+half2)/2;
		}
		return roundDouble(result);
	}
	
	/*M�thode de calcul de la moyenne
	 *somme des valeurs / nombre de valeurs 
	 */
	public static double mean(ValuesList listData){
		double sum=0;
		double iValue;
		int i;
		for(i=0;i<listData.size();i++){
			iValue=listData.get(i);
			sum=sum+iValue;
		}
		return roundDouble(sum/listData.size());
	}
	
	/*M�thode de calcul de l'ecart-type
	 */
	public static double standDeviation(ValuesList listData){
		double sum1;
		double sum2=0;
		double iValue;
		int i;
		for(i=0;i<listData.size();i++){
			iValue=listData.get(i);
			sum1=Math.pow(iValue-mean(listData),2);
			sum2=sum2+sum1;
		}
		return roundDouble(Math.sqrt(sum2/listData.size()));
	}
	
	/*M�thode de calcul de la variance
	 */
	public static double variance(ValuesList listData){
		double sum1;
		double sum2=0;
		double iValue;
		int i;
		for(i=0;i<listData.size();i++){
			iValue=listData.get(i);
			sum1=Math.pow(iValue-mean(listData),2);
			sum2=sum2+sum1;
		}
		return roundDouble(sum2/listData.size());
	}
	
	/*méthode de calcul de la valeur minimum*/
	public static double min(ValuesList listData) {
		return listData.minValue();	
	}
	
	/*méthode de calcul de la valeur maximale*/
	public static double max(ValuesList listData) {
		return listData.maxValue();	
	}
	
	public static double roundDouble(Double result){
		return (double) Math.round(result*100)/100;
	}
}
